package cn.com.adcc.bean;

import cn.com.adcc.utils.Configuration;
import com.sun.management.OperatingSystemMXBean;

import javax.management.MBeanServerConnection;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by hp on 2015/10/13.
 */
@XmlRootElement(name="uploadingstatus")
public class MessageBean {

    private String diskUsageRoot;

    public MessageBean(){

        diskUsageRoot= Configuration.GetValueByKey("config/config.properties", "DISKUSAGEROOT");
        try {
            osMBean = ManagementFactory.newPlatformMXBeanProxy(mbsc, ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME, OperatingSystemMXBean.class);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }



    private MBeanServerConnection mbsc = ManagementFactory.getPlatformMBeanServer();

    private OperatingSystemMXBean osMBean;
    //成功文件个数
    private int successfilecount;
    //未上传的文件个数
    private int nouploadfilecount;

    private int reloadfilecount;
    //正在上传文件
    private  int uploadingfilecount;
    //程序启动时间
    private String runtime="";
    //cpu使用率
    private  String cpuUsed="";
    //内存使用率
    private  String memoryUsed="";
    //硬盘使用率
    private  String diskUsed="";
    //各个任务详细状态
    private Map<String,UploadTaskInfo> uploadTaskInfoMap =new HashMap<String, UploadTaskInfo>();

    public int getUploadingfilecount() {
        return uploadingfilecount;
    }

    public void setUploadingfilecount(int uploadingfilecount) {
        this.uploadingfilecount = uploadingfilecount;
    }

    public int getReloadfilecount() {
        return reloadfilecount;
    }

    public void setReloadfilecount(int reloadfilecount) {
        this.reloadfilecount = reloadfilecount;
    }

    public int getSuccessFileCount() {
        return successfilecount;
    }

    public void setSuccessFileCount(int successFileCount) {
        successfilecount = successFileCount;
    }

    public String getRunTime() {
        return runtime;
    }

    public void setRunTime(String runTime) {
        runtime = runTime;
    }

    public int getNoUploadFileCount() {
        return nouploadfilecount;
    }

    public void setNoUploadFileCount(int noUploadFileCount) {
        nouploadfilecount = noUploadFileCount;
    }

    public String getCpuUsed() {

        cpuUsed=Double.toString(osMBean.getSystemCpuLoad()*100);
        if(cpuUsed.length()>4){
            cpuUsed=cpuUsed.substring(0, 4);
        }
        return cpuUsed;
    }

    public void setCpuUsed(String cpuUsed) {
        this.cpuUsed = cpuUsed;
    }

    public String getMemoryUsed() {
        double used=osMBean.getTotalPhysicalMemorySize()-osMBean.getFreePhysicalMemorySize();
        memoryUsed=Double.toString(used/osMBean.getTotalPhysicalMemorySize()*100);

        if(memoryUsed.length()>4){
            memoryUsed=memoryUsed.substring(0,4);
        }
        return memoryUsed;
    }

    public void setMemoryUsed(String memoryUsed) {
        this.memoryUsed = memoryUsed;
    }

    public String getDiskUsed() {
        File f= new File(diskUsageRoot);
        diskUsed=Long.toString((f.getTotalSpace()-f.getFreeSpace())*100/(f.getTotalSpace()));
        return diskUsed;
    }

    public void setDiskUsed(String diskUsed) {
        this.diskUsed = diskUsed;
    }

    public Map<String, UploadTaskInfo> getUploadTaskInfoMap() {
        return uploadTaskInfoMap;
    }

    public void setUploadTaskInfoMap(Map<String, UploadTaskInfo> uploadTaskInfoMap) {
        this.uploadTaskInfoMap = uploadTaskInfoMap;
    }
}
